#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Python基础：数据结构
"""

def demonstrate_lists():
    """演示列表操作"""
    print("=== 列表示例 ===")
    # 创建列表
    fruits = ["苹果", "香蕉", "橙子"]
    print(f"原始列表: {fruits}")
    
    # 添加元素
    fruits.append("葡萄")
    print(f"添加葡萄后: {fruits}")
    
    fruits.insert(1, "草莓")
    print(f"在索引1插入草莓: {fruits}")
    
    # 删除元素
    fruits.remove("香蕉")
    print(f"删除香蕉后: {fruits}")
    
    # 列表切片
    print(f"前两个元素: {fruits[:2]}")
    print(f"从索引2开始的所有元素: {fruits[2:]}")
    
    # 列表推导式
    numbers = [1, 2, 3, 4, 5]
    squares = [x**2 for x in numbers]
    print(f"数字: {numbers}")
    print(f"平方: {squares}")

def demonstrate_tuples():
    """演示元组操作"""
    print("\n=== 元组示例 ===")
    # 创建元组
    coordinates = (10, 20)
    print(f"坐标元组: {coordinates}")
    print(f"横坐标: {coordinates[0]}, 纵坐标: {coordinates[1]}")
    
    # 元组解包
    x, y = coordinates
    print(f"解包后 - x: {x}, y: {y}")
    
    # 命名元组示例
    from collections import namedtuple
    Point = namedtuple('Point', ['x', 'y'])
    p = Point(100, 200)
    print(f"命名元组: x={p.x}, y={p.y}")

def demonstrate_dictionaries():
    """演示字典操作"""
    print("\n=== 字典示例 ===")
    # 创建字典
    student = {
        "name": "张三",
        "age": 20,
        "major": "计算机科学",
        "grades": [85, 90, 78]
    }
    print(f"学生信息: {student}")
    
    # 访问和修改
    print(f"姓名: {student['name']}")
    student["age"] = 21
    print(f"修改年龄后: {student}")
    
    # 添加新键值对
    student["city"] = "北京"
    print(f"添加城市后: {student}")
    
    # 遍历字典
    print("遍历字典:")
    for key, value in student.items():
        print(f"  {key}: {value}")
    
    # 字典推导式
    squares = {x: x**2 for x in range(1, 6)}
    print(f"字典推导式: {squares}")

def demonstrate_sets():
    """演示集合操作"""
    print("\n=== 集合示例 ===")
    # 创建集合
    set1 = {1, 2, 3, 4, 5}
    set2 = {4, 5, 6, 7, 8}
    print(f"集合1: {set1}")
    print(f"集合2: {set2}")
    
    # 集合运算
    print(f"交集: {set1 & set2}")
    print(f"并集: {set1 | set2}")
    print(f"差集: {set1 - set2}")
    print(f"对称差集: {set1 ^ set2}")
    
    # 集合推导式
    even_squares = {x**2 for x in range(10) if x % 2 == 0}
    print(f"偶数的平方: {even_squares}")

if __name__ == "__main__":
    demonstrate_lists()
    demonstrate_tuples()
    demonstrate_dictionaries()
    demonstrate_sets()